package com.ztgf.sys.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.sys.models.bo.SysInstitutionBO;
import com.ztgf.sys.models.vo.SysAreaVO;
import com.ztgf.sys.models.vo.SysInstitutionVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.ztgf.sys.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.sys.mapper.SysInstitutionMapper;
import com.ztgf.sys.models.service.ISysInstitutionService;
import com.ztgf.sys.models.bo.SysInstitutionSaveBO;
import com.ztgf.sys.models.dto.SysInstitutionDTO;

import java.util.ArrayList;
import java.util.List;


/**
 * 制度表控制器
 *
 * @author YUE
 * @Date 2020-07-04 17:58:52
 */
@DubboService
public class SysInstitutionServiceImpl extends ServiceImpl<SysInstitutionMapper, SysInstitutionDTO> implements ISysInstitutionService{

    @Resource
    private YmlConfig ymlConfig;

    @Override
    public boolean add(SysInstitutionSaveBO saveBO) {
        SysInstitutionDTO dto = new SysInstitutionDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    /**
     * 以树的形式返回信息
     * @return
     */
    @Override
    public Result<List<SysInstitutionVO>> getTreeListHomeInfo(){
        List<SysInstitutionDTO> sysInstitutionDTOList = selectList(new EntityWrapper<>());
        List<SysInstitutionVO> sysInstitutionVOList = new ArrayList<>();
        for (SysInstitutionDTO sysInstitutionDTO : sysInstitutionDTOList) {
            SysInstitutionVO sysInstitutionVO = new SysInstitutionVO();
            BeanUtils.copyProperties(sysInstitutionDTO,sysInstitutionVO);
            sysInstitutionVOList.add(sysInstitutionVO);
        }
        return new Result<>(builTree(sysInstitutionVOList));
    }

    @Override
    public Result<SysInstitutionDTO> getByName(SysInstitutionBO sysInstitutionBO) {
        SysInstitutionDTO sysInstitutionDTO = new SysInstitutionDTO();
        BeanUtils.copyProperties(sysInstitutionBO,sysInstitutionDTO);
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("name",sysInstitutionBO.getName());
        SysInstitutionDTO institutionDTO = selectOne(wrapper);
        return new Result<>(institutionDTO);
    }

    //建立树形结构
    private List<SysInstitutionVO> builTree(List<SysInstitutionVO> sysInstitutionVOList){
        List<SysInstitutionVO> treeMenus =new  ArrayList<>();
        for(SysInstitutionVO menuNode : getRootNode(sysInstitutionVOList)) {
            menuNode=buildChilTree(menuNode,sysInstitutionVOList);
            treeMenus.add(menuNode);
        }
        return treeMenus;
    }

    //递归，建立子树形结构
    private SysInstitutionVO buildChilTree(SysInstitutionVO pNode, List<SysInstitutionVO> areaList){
        List<SysInstitutionVO> chilMenus =new ArrayList<>();
        for(SysInstitutionVO menuNode : areaList) {
            if(menuNode.getParentId().equals(pNode.getId())) {
                chilMenus.add(buildChilTree(menuNode,areaList));
            }
        }
        pNode.setSysInstitutionVOList(chilMenus);
        return pNode;
    }

    //获取根节点
    private List<SysInstitutionVO> getRootNode(List<SysInstitutionVO> areaList) {
        List<SysInstitutionVO> rootMenuLists =new  ArrayList<>();
        for(SysInstitutionVO menuNode : areaList) {
            if(menuNode.getParentId().intValue() == 0) {
                rootMenuLists.add(menuNode);
            }
        }
        return rootMenuLists;
    }
}
